package cn.sail.spring.mvc.controller;

import cn.sail.spring.mvc.content.CodeMsg;
import cn.sail.spring.mvc.vo.ResultVO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MVCExceptionHandlerController {

    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResultVO HandlerRuntimeException(RuntimeException e){
        e.printStackTrace();
        System.out.println("handlerRuntimeExceptions");
        ResultVO resultVO = new ResultVO();
        resultVO.setStatus(CodeMsg.FAIL.getCode());
        resultVO.setMsg(e.getMessage());

        return resultVO;
    }

    @ResponseBody
    @ExceptionHandler({Exception.class})
    public ResultVO handlerExceptions(Exception e) {
        e.printStackTrace();
        System.out.println("handlerExceptions");
        ResultVO resultVO = new ResultVO();
        resultVO.setStatus(CodeMsg.FAIL.getCode());
        resultVO.setMsg(e.getMessage());
        return resultVO;
    }
}
